package com.uade.services;

import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.log4j.Logger;

import com.uade.model.Configuracion;

public class LogJMS extends LogSenderStrategy {
	private Logger log = Logger.getLogger(LogSenderStrategy.class);


	@Resource(mappedName="jms/ConnectionFactory")
	private static ConnectionFactory connectionFactory;

	@Resource(mappedName="jms/log")
	private static Queue queue;
	
	public LogJMS(Configuracion conf) {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void enviarLog(String xml) {
		try{
			log.info("Se envia el xml: " + xml);
			String DEFAULT_DESTINATION = "jms/queue/log";
		    String DEFAULT_USERNAME = "san";
		    String DEFAULT_PASSWORD = "123";
		    String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
		    String PROVIDER_URL = "remote://localhost:4447";
		    String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
			 
	        Context context = null;
	        final Properties env = new Properties();
	        env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
	        env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
	        env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
	        env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
	        env.put("jboss.naming.client.ejb.context", true);
	        env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
	        
	        log.info("Se genera el context");
	        context = new InitialContext(env);

	        // Perform the JNDI lookups
	        String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
	        connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
	        log.info("Se genera el connection Factory");
			Connection connection = connectionFactory.createConnection();
			log.info("Se genera el connection");
//	        Connection connection = connectionFactory.createConnection(DEFAULT_USERNAME, DEFAULT_PASSWORD);
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			log.info("Se genera la session");
			Destination d=(Destination) context.lookup(DEFAULT_DESTINATION);
			log.info("Se genera el producer");
			MessageProducer messageProducer = session.createProducer(d);
			connection.start();
			
			TextMessage message = session.createTextMessage();
			message.setText(xml);
			log.info("Envio el mensaje");
			messageProducer.send(message);
			System.out.println(xml);
			System.out.println("Se envio el mensaje correctamente");
			
			connection.close();
		}catch(Exception ex){
			log.info("Se produjo un error en el envio de Log JMS a monitoreo");
			ex.getStackTrace();
		}

		
	}

}
